﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using AssessmentActions;
using TestingLibrary;

namespace TestServiceProject
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class TestService : ITestService
    {

        /// <summary>
        /// Query the database for all multiple choice questions
        /// </summary>
        /// <returns></returns>
        public string[] getAllMCQuestions()
        {
            List<string> question_list = new List<string>();

            // Connect to the database
            string strConnString =
            System.Configuration.ConfigurationManager.ConnectionStrings
                ["dbConnectionString"].ConnectionString;

            // JSC TODO: Use web service instead
            AssessmentActions.AssessmentDatabaseActions ada =
            new AssessmentActions.AssessmentDatabaseActions(strConnString);

            List<AssessmentActions.MultipleChoiceQuestion> questions = ada.getAllMultipleChoiceFromDatabase();

            foreach (AssessmentActions.MultipleChoiceQuestion q in questions)
            {
                // Serialize the assessment for storage in the database
                MemoryStream memoryStream = new MemoryStream();
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, q);
                string serialized_question = Convert.ToBase64String(memoryStream.ToArray());

                question_list.Add(serialized_question);
            }

            return question_list.ToArray();
        }

        /// <summary>
        /// Query the database for all true false questions
        /// </summary>
        /// <returns></returns>
        public string[] getAllTFQuestions()
        {
            
            //// Connect to the database
            //string strConnString =
            //System.Configuration.ConfigurationManager.ConnectionStrings
            //    ["dbConnectionString"].ConnectionString;

            //// JSC TODO: Use web service instead
            //AssessmentActions.AssessmentDatabaseActions ada =
            //new AssessmentActions.AssessmentDatabaseActions(strConnString);

            //return ada.getAllTrueFalseFromDatabase();

            List<string> question_list = new List<string>();

            // Connect to the database
            string strConnString =
            System.Configuration.ConfigurationManager.ConnectionStrings
                ["dbConnectionString"].ConnectionString;

            // JSC TODO: Use web service instead
            AssessmentActions.AssessmentDatabaseActions ada =
            new AssessmentActions.AssessmentDatabaseActions(strConnString);

            List<AssessmentActions.TrueFalseQuestion> questions = ada.getAllTrueFalseFromDatabase();

            foreach (AssessmentActions.TrueFalseQuestion q in questions)
            {
                // Serialize the assessment for storage in the database
                MemoryStream memoryStream = new MemoryStream();
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, q);
                string serialized_question = Convert.ToBase64String(memoryStream.ToArray());

                question_list.Add(serialized_question);
            }
            return question_list.ToArray();
        }

        /// <summary>
        /// Query the database for all test subjects
        /// </summary>
        /// <returns></returns>
        public string[] getAllSubjects()
        {
            // Connect to the database
            string strConnString =
            System.Configuration.ConfigurationManager.ConnectionStrings
                ["dbConnectionString"].ConnectionString;

            // JSC TODO: Use web service instead
            AssessmentActions.AssessmentDatabaseActions ada =
            new AssessmentActions.AssessmentDatabaseActions(strConnString);

            List<string> subject_list = ada.getAllTestSubjects();            
            return subject_list.ToArray();
        }

        /// <summary>
        /// Add an assessment to the database
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="assessment"></param>
        public void addAssessment(string subject, string serialized_assessment)
        {
            // Connect to the database
            string strConnString =
            System.Configuration.ConfigurationManager.ConnectionStrings
                ["dbConnectionString"].ConnectionString;

            // JSC TODO: Use web service instead
            AssessmentActions.AssessmentDatabaseActions ada =
            new AssessmentActions.AssessmentDatabaseActions(strConnString);

            // create the new assessment
            ada.addSerializedAssessmentToDatabase(subject, serialized_assessment);
        }
    }
}
